package cn.com.generaldata.os.common;

import javax.servlet.ServletContext;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

@InjectName
@Component
public class GlobalSetting implements ServletContextAware {

	/**
	 * 当前工程运行模式：LAN或WEB. 默认值在global_conf.properties中配置
	 */
	@Value("${projectRunMode}")
	private String projectRunMode;

	public String getProjectRunMode() {
		return projectRunMode;
	}

	public void setProjectRunMode(String projectRunMode) {
		this.projectRunMode = projectRunMode;
	}

	public GlobalSetting() {

	}

	@At("/runMode/show")
	@Ok("jsp:/static/runMode")
	public String showRunMode() {
		return this.projectRunMode;
	}

	@At("/runMode/switch")
	@Ok(">>:/runMode/show")
	public void switchRunMode(@Param("mode") String newMode) {
		if (newMode != null && newMode.length() > 0) {
			if (newMode.equals("LAN")) {
				this.projectRunMode = "LAN";
			} else {
				this.projectRunMode = "WEB";
			}
		}
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		servletContext.setAttribute("runMode", projectRunMode);
	}

}
